<template>
	<view class="user-coupon">
		<view class="tabs">
			<view class="tabs-item" :class="{'active':item.type == currTab.type}" v-for="item in coupons"
				:key="item.type" @click="chageTab(item)">
				<template v-if="item.type == 0">
					<text>{{currType.name || item.title}}</text>
					<u-icon class="ml10" size="24" :name="selectShow?'arrow-up':'arrow-down'"></u-icon>
				</template>
				<template v-else>
					{{item.title}}
				</template>
			</view>
		</view>

		<view class="main-box">
			<view class="item" v-for="(item, index) in couponList" :key="index">
				<image v-if="item.type == 1" src="../../static/images/coupon_type_1.png" mode="widthFix"
					class="type-icon"></image>
				<image v-if="item.type == 2" src="../../static/images/coupon_type_2.png" mode="widthFix"
					class="type-icon"></image>
				<image v-if="item.type == 3" src="../../static/images/coupon_type_3.png" mode="widthFix"
					class="type-icon"></image>
				<view class="top-bar">
					<view class="left">
						<image :src="item.img" mode="widthFix" class="icon"></image>
						<view class="info">
							<text class="text">{{item.name}}</text>
							<text class="date">{{item.stat_day}}-{{item.end_day}}</text>
						</view>
					</view>
					<view class="right">
						<price-format color="var(--primary-price-color)" :first-size="56" :subscript-size="30"
							:price="item.amount" :weight="500"></price-format>
						<text class="text">满{{item.enough}}减{{item.amount}}</text>
					</view>
				</view>

				<image src="../../static/images/line.png" mode="" class="line-img"></image>

				<view class="footer-bar">
					<view class="left">
						<text class="desc">{{item.type_str}}</text>
						<view class="btn" @tap="onToggleItemDesc(item, index)">
							使用说明
							<u-icon v-if="!item.isOpen" class="ml10" name="arrow-down" color="#2979ff"
								size="28"></u-icon>
							<u-icon v-else class="ml10" name="arrow-up" color="#2979ff" size="28"></u-icon>
						</view>
					</view>
					<view class="right">
						<view v-if="item.order_str=='待使用'" @tap="goUse(item.type)" class="btn btn1">去使用</view>
						<view v-else>{{item.order_str}}</view>
					</view>
				</view>

				<view class="context" v-if="item.isOpen">
					<view class="row">
						<view class="label">使用时间：</view>
						<view class="text">{{item.start_day}}-{{item.end_day}}</view>
					</view>
					<view class="row">
						<view class="text">{{item.description}}</view>
					</view>
				</view>
			</view>

			<loading-footer :status="status"></loading-footer>
		</view>

		<u-popup v-model="selectShow" mode="top" :safe-area-inset-bottom="true" :border-radius="30">
			<view class="list-item" v-for="item in options1" :key="item.type"
				:class="{'active':item.type==currType.type}" @tap="checkedData(item)">
				<text>{{item.name}}</text>
				<u-icon v-if="currType.type == item.type" name="checkmark-circle-fill"></u-icon>
			</view>
		</u-popup>
	</view>
</template>

<script>
	const options1 = [{
		name: '全部',
		type: 0
	}, {
		name: '零售券',
		type: 1
	}, {
		name: '租赁券',
		type: 2
	}, {
		name: '充值券',
		type: 3
	}]
	const tabList = [{
		label: '全部',
		type: 0
	}, {
		title: '待使用',
		type: 1
	}, {
		title: '已使用',
		type: 2
	}, {
		title: '已过期',
		type: 3
	}];
	import {
		getMyCouponList
	} from '../../../api/user';
	import {
		loadingFun
	} from '../../../utils/tools';
	import {
		loadingType
	} from '../../../utils/type';
	export default {
		data() {
			return {
				coupons: tabList,
				options1: options1,
				currTab: tabList[0],
				currType: options1[0],
				selectShow: false,
				couponList: null,
				page: 1,
				status: loadingType.LOADING,
			};
		},

		components: {

		},
		props: {},
		onLoad() {
			this.getMyCouponFun();
		},
		onReachBottom() {
			this.getMyCouponFun();
		},
		methods: {
			goUse(type) {
				let url = '';
				switch (type) {
					case 1:
						url = "/pages/sort/sort"
						break;
					case 2:
						url = "/package_goods/pages/goods_list/index"
						break;
					case 3:
						url = "/package_user/pages/user_vip/index"
						break;
				}
				uni.navigateTo({
					url
				})
			},
			checkedData(data) {
				if (data.type != this.currType.type)
					this.currType = data
				this.clear();
				this.getMyCouponFun();
				this.selectShow = false;
			},
			chageTab(tab) {
				if (tab.type != this.currTab.type) {
					this.currTab = tab;
					this.clear();
					this.getMyCouponFun();
				} else {
					if (tab.type == 0) {
						this.selectShow = true;
					}
				}
			},
			clear() {
				this.page = 1;
				this.status = loadingType.LOADING;
				this.couponList = [];
			},
			onToggleItemDesc(item, index) {
				this.couponList[index].isOpen = !item.isOpen
			},
			async getMyCouponFun() {
				const data = await loadingFun(getMyCouponList, this.page, this.couponList, this.status, {
					type: this.currType.type,
					status: this.currTab.type,
				})
				if (data) {
					this.page = data.page;
					this.status = data.status;
					this.couponList = data.dataList.map(v => ({
						...v,
						isOpen: v.isOpen ?? false
					}));;
				}
			}
		}
	};
</script>
<style lang="scss">
	.tabs {
		display: flex;
		width: 100%;
		height: 80rpx;
		background-color: white;

		.tabs-item {
			flex: 1;
			line-height: 80rpx;
			text-align: center;

			&.active {
				color: var(--primary-color);
			}
		}
	}

	.list-item {
		display: flex;
		width: 100%;
		justify-content: space-between;
		padding: 20rpx;

		&:not(:last-of-type) {
			border-bottom: 2rpx solid #eee;
		}

		&.active {
			color: var(--primary-color)
		}
	}

	.main-box {
		overflow: auto;

		.item {
			width: 710rpx;
			background: #FFFFFF;
			border-radius: 20rpx;
			position: relative;
			margin: 20rpx auto;

			.type-icon {
				width: 96rpx;
				height: 96rpx;
				position: absolute;
				top: 0;
				left: 0;
				z-index: 2;
			}

			.top-bar {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 40rpx 28rpx;

				.left {
					display: flex;
					align-items: center;

					.icon {
						width: 100rpx;
						height: 100rpx;
						margin-right: 20rpx;
					}

					.info {
						height: 100rpx;
						padding: 10rpx 0;
						display: flex;
						flex-direction: column;
						justify-content: space-between;

						.text {
							font-family: PingFang SC, PingFang SC;
							font-weight: 600;
							font-size: 32rpx;
							color: #333333;
							line-height: 32rpx;
						}

						.date {
							font-family: PingFang SC, PingFang SC;
							font-weight: 400;
							font-size: 24rpx;
							color: #333333;
							line-height: 24rpx;
						}
					}
				}

				.right {
					display: flex;
					flex-direction: column;
					align-items: flex-end;

					.text {
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 24rpx;
						color: var(--primary-price-color);
						line-height: 24rpx;
						text-align: right;
					}
				}
			}

			.line-img {
				width: 654rpx;
				height: 1px;
				margin: 0 auto;
				display: block;
			}

			.footer-bar {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 20rpx 28rpx;

				.left {
					display: flex;
					align-items: center;

					.desc {
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 24rpx;
						color: #666666;
						line-height: 24rpx;
					}

					.btn {
						display: flex;
						align-items: center;
						margin-left: 28rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 24rpx;
						color: #1E8AF4;
						line-height: 24rpx;
					}
				}

				.right {
					.btn {
						width: 152rpx;
						height: 56rpx;
						border-radius: 120rpx;
						font-family: PingFang SC, PingFang SC;
						font-weight: 500;
						font-size: 24rpx;
						line-height: 24rpx;
						display: flex;
						align-items: center;
						justify-content: center;
					}

					.btn1 {
						background: #1E8AF4;
						color: #FFFFFF;
					}

					.btn2 {
						border: 1rpx solid #1E8AF4;
						color: #1E8AF4;
					}
				}
			}

			.context {
				width: 654rpx;
				padding: 28rpx 0;
				margin: 0 auto;
				border-top: 1rpx solid #E6E6E6;

				.row {
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #666666;
					line-height: 36rpx;
				}
			}
		}
	}
</style>